package com.xjlsoft.weather.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.xjlsoft.weather.contant.Constants;
import com.xjlsoft.weather.dto.ResultEntity;
import com.xjlsoft.weather.enums.ResultCodeEnum;
import com.xjlsoft.weather.exception.WeatherException;

/**
 * 结果处理工具类
 *
 * @author 1919
 */
public class ResultUtil {

    
    /**
     * 根据返回结果枚举   产生结果集  success标志为 false
     * */
    public static Map<String, Object> resultMap(ResultCodeEnum resultCodeEnum) {
        return resultMap(resultCodeEnum.getValue(),resultCodeEnum.getMessage(), null,false);
    }
    
    /**
     * 根据返回code  message <br/>
     * 无数据,false
     * */
    public static Map<String, Object> resultMap(String code,String message) {
        return resultMap(code,message, null,false);
    }
    
    
    /**
     * 根据异常信息  产生结果集  success标志为 false
     * */
    public static Map<String, Object> resultMap(WeatherException expressException) {
        return resultMap(expressException.getErrorCode(), expressException.getMessage(), null,false);
    }
    
    /**
     * 根据 ResultEntity  返回  结果集
     * */
    public static Map<String, Object> resultMap(ResultEntity resultEntity) {
        return resultMap(resultEntity.getEchoCode(), resultEntity.getEchoDes(), resultEntity.getData(),resultEntity.getSuccess());
    }


    /**
     * 结果信息
     *
     * @param resultCode
     * @param resultMsg
     * @param result
     * @return
     */
    private static Map<String, Object> resultMap(String code, String msg, Object data,boolean successFlag) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put(Constants.RESULT_CODE, code);
        resultMap.put(Constants.RESULT_MSG, StringUtils.isNotBlank(msg) ? msg : "");
        if (data != null) {
            resultMap.put(Constants.RESULT_DATA, data);
        }else{  //
        	resultMap.put(Constants.RESULT_DATA,"");
        }
        resultMap.put(Constants.RESULT_SUCCESS,successFlag);
        return resultMap;
    }
}
